<?php
namespace App\Repositories\File;
use Symfony\Component\Process\Exception\LogicException;
use Illuminate\Support\Facades\Storage;
use Cache;

class FileRepository
{
    public function upload($file, $fileType="default"){
        if ((!$file)) {
            throw new LogicException('没有找到上传的文件');
        }
        $extendsions = config('file.allowFileExtension');
        $originalExtension = $file->getClientOriginalExtension();
        $extendName = strtolower($file->getClientOriginalExtension());
        if ( !isset($extendsions[$extendName]) ) {
            throw new LogicException('不支持的文件类型');
        }

        $key = sha1(Cache::increment('file_upload_key') . str_random());
        $path = storage_path() . '/../public/' . $fileType;
        if (!file_exists($path)) {
            mkdir($path, 777);
        }
        $file->move($path, $key . '.jpg');
        $url = env('APP_URL') . '/' . $fileType . '/' . $key . '.' . $originalExtension;

        return $url;
    }
}
